<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-button type="primary">添加角色</el-button>
            <el-table
                :data="jueSe"
                style="width: 100%"
                border
                stripe
            >
            <!--  -->
                <el-table-column
                width="48px"
                type="expand"
                >
                    <template v-slot="scope">
                    <el-row :class="['bd-bottom','vcenter', index === 0 ? 'bd-top' : '']" v-for="(item1,index) in scope.row.children" :key="index">
                        <el-col :span="5">
                            <el-tag>{{item1.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <el-col :span="19">
                            <el-row :class="['bd-top','vcenter', index1===0 ? '' : 'bd-top']" v-for="(items,index1) in item1.children" :key="index1">
                                <el-col :span="6">
                                    <el-tag type="success">{{items.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="18">
                                    <el-tag closable @close="removeRights(scope.row,item2.id)" type="warning" v-for="(item2 ,index2) in items.children" :key="index2">

                                            {{item2.authName}}
                                    </el-tag>
                                    
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                    </template>
                </el-table-column>
            <!-- 序号 -->
                <el-table-column
                    label="#"
                    type="index"

                >
                </el-table-column>

                <!-- 角色名称 -->
                <el-table-column
                    prop="roleName"
                    label="角色名称"
                >
                </el-table-column>

                <!-- 角色描述 -->
                <el-table-column
                    prop="roleDesc"
                    label="角色描述"
                >
                </el-table-column>

                <!-- 操作 -->
                <el-table-column
                    label="操作"
                >
                    <template v-slot="scope">
                        <el-button size="mini"  icon="el-icon-edit" type="primary">编辑</el-button>
                        <el-button size="mini" icon="el-icon-delete" type="danger">删除</el-button>
                        <el-button size="mini" icon="el-icon-setting" type="warning" @close="showSetRightClose" @click="showSetRight(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分配权限的对话框 -->
            <el-dialog
                title="分配权限"
                :visible.sync="dialogRight"
                width="50%"
            >
                <!-- 树形控件 -->
                <el-tree ref="treeRef" :data="rightList" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys"></el-tree>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogRight = false">取 消</el-button>
                    <el-button type="primary" @click="aliotRights()">确 定</el-button>
                </span>
            </el-dialog>
    
        </el-card>
    </div>
</template>
<script>
export default {
    data() {
        return {
            jueSe:[],
            dialogRight:false,
            rightList:[],
            //树形控件的绑定对象
            treeProps:{
                label:'authName',
                children:'children'
            },
            defKeys:[],
            roleId:''
        }
    },
    methods: {

        //获取角色列表
        async HuoJue(){
            const { data } = await this.axios.get('roles')
            //console.log(data)
            this.jueSe=data.data
        },
        //根据id删除对应的权限
        async removeRights(role, val){
            //弹框是否删除？
            const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '删除权限', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err=>err)
            if(confirm !== 'confirm'){
                return this.$message.info('取消了删除')
            }
            if(confirm == 'confirm'){
                const { data } = await this.axios.delete(`roles/${role.id}/rights/${val}`)
                //console.log(data)
                role.children = data.data;
            }
        },
        //展示对话权限的对话框
        async showSetRight(role){
            this.roleId = role.id;
            //获取所有权限的数据
            const { data } = await this.axios.get('rights/tree')
            //console.log(data)
            this.dialogRight= true
            this.rightList = data.data

            //递归获取三级节点Id
            this.getLeafkeys(role,this.defKeys)
        },
        getLeafkeys(node,arr){
            //如果node不包含children属性，则是三级节点
            if(!node.children){
                return arr.push(node.id)
            }
            node.children.forEach(item=>this.getLeafkeys(item,arr))
        },
        showSetRightClose(){
            this.defKeys=[]
        },
        //分配权限
        async aliotRights(){
            this.dialogRight = false
            const key=[
                ...this.$refs.treeRef.getCheckedKeys(),
                ...this.$refs.treeRef.getHalfCheckedKeys()
            ]
            const str = key.join(',')
            const { data } = await this.axios.post(`roles/${this.roleId}/rights`,{rids:str})
            console.log(data)
            //console.log(key)

        }
    },
    created() {
        this.HuoJue()
    },
}
</script>
<style lang="less" scoped>
    .el-table{
        margin-top: 20px;
    }
    .el-tag{
        margin: 7px;
    }
    .bd-top{
        border-top: 1px solid #eee;
    }
    .bd-bottom{
        border-bottom: 1px solid #eee;
    }
    .vcenter{
        display: flex;
        align-items: center;

    }
</style>